<?php
namespace app\cms\controller;

use think\Db;
use think\Exception;

class Product
{
    /*
     * 商品同步 每分钟执行1次
     * 2017/8/8 蒋佳林 ADD
     *
     * */
    public function run()
    {
        Db::connect('homegarden_cms');
        $date = Date('Y-m-d H:i:s', time());

        // 先执行下线操作
        $off_list = Db::table('homegarden_timer.product')->where('off_time', 'ELT', $date)
            ->where('off_status', 0)->column('id');

        if ($off_list) {
            Db::startTrans();
            try {
                // 更新homegarden.timer.product
                DB::table('homegarden_timer.product')->where('id', 'IN', $off_list)
                    ->update(['off_time' => date('Y-m-d H:i:s'), 'off_status' => '1']);
                // 更新homegarden_cms.product
                DB::table('homegarden_cms.product')->where('id', 'IN', $off_list)->setField('is_used', 2);
                // 更新homegarden.product
                DB::table('homegarden.product')->where('id', 'IN', $off_list)->setField('issale', 0);
                // 清空购物车
                $product_id = DB::table('homegarden.product')->where('id', 'IN', $off_list)->column('product_id');
                DB::table('homegarden.shoppingcart')->where('product_id', 'IN', $product_id)->delete();
            } catch (Exception $e) {
                DB::rollback();
                return $e->getMessage();
            }
            DB::commit();
        }
        // 后执行上线操作
        $on_list = DB::table('homegarden_timer.product')->where('on_time', 'ELT', $date)
            ->where('on_status', 0)->where('off_status', 0)->column('id');
        if ($on_list) {
            DB::startTrans();
            try {
                // 更新homegarden.timer.product
                DB::table('homegarden_timer.product')->where('id', 'IN', $on_list)
                    ->update(['on_time' => date('Y-m-d H:i:s'), 'on_status' => '1']);
                // 更新homegarden_cms.product
                DB::table('homegarden_cms.product')->where('id', 'IN', $on_list)
                    ->setField('is_used', 1);
                // 更新homegarden.product
                $add = DB::table('homegarden_cms.product')->alias('cp')
                    ->join('homegarden_cms.product_stock s', 's.product_code = cp.code', 'LEFT')
                    ->join('homegarden.product p', 'p.product_id = cp.code', 'LEFT')
                    ->where('p.id', 'EXP', 'is null')
                    ->where('cp.id', 'IN', $on_list)->field('
                    cp.id,
                    cp.code product_id,
                    cp.name product_name,
                    cp.price product_price,
                    cp.main_price product_mainPrice,
                    cp.standard product_standard,
                    cp.brief_description product_briefDescription,
                    cp.description product_description,
                    s.stock product_stock,
                    cp.number product_number,
                    cp.unit product_unit,
                    cp.category_class1 product_class1,
                    cp.category_class2 product_class2,
                    cp.category_class3 product_class3,
                    cp.thumb_image product_headImage,
                    cp.head_image product_bannerImage,
                    cp.detail_image product_detailImage,
                    cp.corner_image theme_coverImage,
                    cp.bottom_image theme_bottomImage,
                    cp.real_weight real_weight,
                    cp.is_used issale,
                    cp.address,
                    cp.cycle,
                    cp.reposition,
                    cp.is_coupon,
                    cp.is_integral
                ')->select();
                DB::table('homegarden.product')->insertAll($add);
                // 更新homegarden.product
                $edit = DB::table('homegarden_cms.product')->alias('cp')
                    ->join('homegarden.product p', 'p.product_id = cp.code', 'LEFT')
                    ->where('p.id', 'EXP', 'is not null')
                    ->where('cp.id', 'IN', $on_list)->field('
                    cp.id,
                    cp.code product_id,
                    cp.name product_name,
                    cp.price product_price,
                    cp.main_price product_mainPrice,
                    cp.standard product_standard,
                    cp.brief_description product_briefDescription,
                    cp.description product_description,
                    cp.number product_number,
                    cp.unit product_unit,
                    cp.category_class1 product_class1,
                    cp.category_class2 product_class2,
                    cp.category_class3 product_class3,
                    cp.thumb_image product_headImage,
                    cp.head_image product_bannerImage,
                    cp.detail_image product_detailImage,
                    cp.corner_image theme_coverImage,
                    cp.bottom_image theme_bottomImage,
                    cp.real_weight real_weight,
                    cp.is_used issale,
                    cp.address,
                    cp.cycle,
                    cp.reposition,
                    cp.is_coupon,
                    cp.is_integral
                ')->select();
                foreach ($edit as $e) {
                    DB::table('homegarden.product')->where("product_id=".$e['product_id'])->update($e);
                }
            } catch (Exception $e) {
                DB::rollback();
                return $e->getMessage();
            }
            DB::commit();
        }
    }
}
